package com.sskj.gocoin.v.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.TabItem;
import com.sskj.gocoin.common.adapter.MyFragmentPagerAdapter;
import com.sskj.gocoin.m.bean.TradeInfoBean;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.gocoin.p.activity.TradePresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.TipUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.gocoin.v.fragment.NewTradePositionFragment;
import com.sskj.gocoin.v.fragment.TradeDealFragment;
import com.sskj.gocoin.v.fragment.TradeDeputeFragment;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 交易订单界面
 * <pre>
 *     author : 吕志豪
 *     e-mail : 1030753080@qq.com
 *     time   : 2018/04/04
 *     desc   :
 *     version: 1.0
 * </pre>
 */
@Deprecated
public class TradeActivity extends BaseActivity<TradePresenter> {
    @BindView(R2.id.slidingTabLayout)
    CommonTabLayout slidingTabLayout;
    @BindView(R2.id.tv_trade_id)
    TextView tvTradeId;
    @BindView(R2.id.iv_back)
    ImageView ivBack;
    @BindView(R2.id.iv_right)
    ImageView ivRight;
    @BindView(R2.id.trade_top_a)
    TextView mTradeTopA;
    @BindView(R2.id.trade_top_b)
    TextView mTradeTopB;
    @BindView(R2.id.trade_top_c)
    TextView mTradeTopC;
    @BindView(R2.id.trade_top_d)
    TextView mTradeTopD;  // 浮动盈亏
    @BindView(R2.id.textView57)
    TextView textView57;  // 浮动盈亏
    @BindView(R2.id.textView59)
    TextView textView59;  // 浮动盈亏
    @BindView(R2.id.trade_top_e)
    TextView mTradeTopE;
    @BindView(R2.id.trade_top_f)
    TextView mTradeTopF;
    @BindView(R2.id.clean_order_btn)
    Button mCleanOrderBtn;  // 一键平仓
    private MyFragmentPagerAdapter mAdapter;
    private ArrayList<Fragment> mFragments;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_trade;
    }

    @Override
    public TradePresenter getPresenter() {
        return new TradePresenter();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.cancel();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.getTradeInfo();
    }

    @Override
    protected void onViewBinding() {
        ClickUtil.click(ivBack, () -> finish());

        ClickUtil.click(mCleanOrderBtn, () -> {
            TipUtil.getTip(mActivity, App.INSTANCE.getString(R.string.strTradeActivity200), App.INSTANCE.getString(R.string.strTradeActivity201), () -> {
                mPresenter.pingcangAll();
            }).show();
        });

        ivRight.setVisibility(View.GONE);
        int index = getIntent().getIntExtra("index", 0);
        if (index == 0) {
            mCleanOrderBtn.setVisibility(View.VISIBLE);
            mCleanOrderBtn.setEnabled(true);
        } else {
            mCleanOrderBtn.setVisibility(View.GONE);
            mCleanOrderBtn.setEnabled(false);
        }
        UserInfo mUserInfo = (UserInfo) SPUtils.getBean(this, Constants.SP_USER_INFO);
        setText(tvTradeId, App.INSTANCE.getString(R.string.strTradeActivity203) + mUserInfo.account);

        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.strTradeActivity204), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.strTradeActivity205), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.strTradeActivity206), 0, 0));
        mFragments = new ArrayList<>();
        // 持仓
        Bundle args = new Bundle();
        NewTradePositionFragment newTradePositionFragment = new NewTradePositionFragment();
        args.putString("code", "");
        newTradePositionFragment.setArguments(args);
        mFragments.add(newTradePositionFragment);  // 持仓
        Bundle argwt = new Bundle();
        TradeDeputeFragment tradeDeputeFragment = new TradeDeputeFragment();// 委托
        args.putString("code", "");
        tradeDeputeFragment.setArguments(argwt);
        mFragments.add(tradeDeputeFragment);
        mFragments.add(new TradeDealFragment());  // 成交
        slidingTabLayout.setTabData(tabItems, this, R.id.trade_content, mFragments);
        slidingTabLayout.setCurrentTab(index);
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {



                if (position == 0) {
                    mCleanOrderBtn.setVisibility(View.VISIBLE);
                    mCleanOrderBtn.setEnabled(true);
                } else {
                    mCleanOrderBtn.setVisibility(View.GONE);
                    mCleanOrderBtn.setEnabled(false);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    public void updateTrade(TradeInfoBean bean) {
        mTradeTopA.setText(bean.getTotalusdt());
        mTradeTopB.setText(bean.getKeyong_price());
        mTradeTopC.setText(bean.getTotaldeposit());
        mTradeTopD.setText(bean.getYingkui());
        if (bean.getYingkui().contains("-")) {
            mTradeTopD.setTextColor(getResources().getColor(R.color.market_down));
            textView57.setTextColor(getResources().getColor(R.color.market_down));
        } else {
            mTradeTopD.setTextColor(getResources().getColor(R.color.market_up));
            textView57.setTextColor(getResources().getColor(R.color.market_up));
        }
        // 安全系数
        mTradeTopE.setText(bean.getRisk());
//        if (bean.getPingcang().contains("-")) {
//            mTradeTopE.setTextColor(getResources().getColor(R.color.market_down));
//            textView59.setTextColor(getResources().getColor(R.color.market_down));
//        } else {
//            mTradeTopE.setTextColor(getResources().getColor(R.color.market_up));
//            textView59.setTextColor(getResources().getColor(R.color.market_up));
//        }
//        mTradeTopF.setText(bean.getRisk());
        updatefragment();
    }

    public void updatefragment() {
        ((NewTradePositionFragment) mFragments.get(0)).update();
    }

    @Override
    protected void onDestroy() {
        mPresenter.cancel();
        super.onDestroy();
    }
}
